package net.huashitong.supervision.task.dao;

import com.sirdc.modules.core.dao.GenericQuery;
import com.sirdc.modules.core.dao.StringDao;
import com.sirdc.modules.core.entity.StringEntity;
import com.sirdc.modules.core.filter.Paging;
import com.sirdc.modules.utils.ObjectUtils;
import com.sirdc.modules.utils.StringUtils;
import net.huashitong.supervision.task.entity.CollectEntity;
import net.huashitong.supervision.task.entity.TaskMain;
import net.huashitong.supervision.task.filter.CollectFilter;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: ling
 * Date: 2019/12/25
 * Time: 17:54
 * To change this template use File | Settings | File Templates.
 * Description:
 */
@Repository
public class CollectDao extends StringDao<CollectEntity> {
    /**
     * 根据用户id和收藏类型查询列表
     * @param filter
     * @return
     */
    public List<CollectEntity> getByUserIdAndType(CollectFilter filter) {
        GenericQuery obj = createQuery("obj");
        obj.append(" and  del =:del").setParam("del", "1");
        obj.append( " and type =:type").setParam("type", filter.getType());
        obj.append(" and userId =:userId").setParam("userId", filter.getUserId());
        if (StringUtils.isNotBlank(filter.getTaskName())){
            obj.append(" and taskName like :taskName").setParam("taskName", StringUtils.getLikewords(filter.getTaskName()));
        }
        if (StringUtils.isNotBlank(filter.getTaskId())){
            obj.append(" and taskId = :taskId").setParam("taskId", filter.getTaskId());
        }
        //设置排序
        if (ObjectUtils.isNotBlank(filter.getPaging())) {
            if (StringUtils.isBlank(filter.getPaging().getSortCol())) {
                filter.getPaging().setSortCol("sysId");
                filter.getPaging().setSortVal("desc");
            }
        } else {
            Paging paging = new Paging(0);
            paging.setSortCol("sysId");
            paging.setSortVal("desc");
            filter.setPaging(paging);
        }
        List<CollectEntity> list = obj.listResult(filter);
        return list;
    }

    public List<CollectEntity> getByUserIdAndTaskId(String userId, String sysId,String del) {
        GenericQuery obj = createQuery("obj");
        obj.append(" and userId =:userId").setParam("userId", userId);
        obj.append(" and taskId =:taskId").setParam("taskId", sysId);
        if (StringUtils.isNotBlank(del)) {
            obj.append(" and  del =:del").setParam("del", del);
        }
        return obj.listResult();
    }
}
